// pages/withdrawResult/withdrawResult.js
var util = require('../../../utils/util.js')
var app = getApp()
var rootUrl = app.globalData.rootDocment
var imgPublicUrl = rootUrl + 'Public/Home/images/'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbar: ['全部', '提现中', '已到账'],
    currentNavbar: 0,
    imageArr: {
      withdrawIcon: imgPublicUrl + 'withdrawIcon.png',
      realizedAccountIcon: imgPublicUrl + 'realizedAccountIcon.png',
      statusIcon: imgPublicUrl + 'statusIcon.png',
      serviceChargeIcon: imgPublicUrl + 'serviceChargeIcon.png',
      TimeIcon: imgPublicUrl + 'TimeIcon.png'
    },
      //全部
      allWithdrawResultData:[],
      //提现中
      WithdrawResultingData: [],
      //已到账
      WithdrawResultedData: [],

      allWithdrawResultDataStatus:false,   //全部暂无数据， true为没有数据，false为有数据
      WithdrawResultingDataStatus:false, //提现中暂无数据，true为没有数据，false为有数据
      WithdrawResultedDataStatus:false,  //已到账暂无数据，true为没有数据，false为有数据

      allpage: 1,   // 设置加载的第几次，默认是第一次
      ingpage: 1,   // 设置加载的第几次，默认是第一次
      endpage: 1,   // 设置加载的第几次，默认是第一次

      upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
      upslideNoData: true,	//“没有数据”的变量，默认true，隐藏

      ifRefresh: false,	//是否是下拉刷新
    },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var currentNavbar = this.data.currentNavbar
    if (currentNavbar == 0) {
      //获取全部提现信息列表
      this.getAllWithdrawList()
    } else if (currentNavbar == 1) {
      //获取提现中提现信息列表
      this.getIngWithdrawList()
    } else if (currentNavbar == 2) {
      //获取已到账提现信息列表
      this.getEndWithdrawList()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
	  app.publicchangenavigate()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    var that = this
    wx.showNavigationBarLoading() //在标题栏中显示加载
    //初始化参数
    that.setData({
      allpage: 1,   // 设置加载的第几次，默认是第一次
      ingpage: 1,   // 设置加载的第几次，默认是第一次
      endpage: 1,   // 设置加载的第几次，默认是第一次
      upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
      upslideNoData: true,	//“没有数据”的变量，默认true，隐藏 
      ifRefresh: true,
    })
    //模拟加载
    setTimeout(function () {
      var currentNavbar = that.data.currentNavbar
      if (currentNavbar == 0) {
        that.setData({ allpage: 1})
        that.getAllWithdrawList()
      } else if (currentNavbar == 1) {
        that.setData({ ingpage: 1 })
        that.getIngWithdrawList()
      } else if (currentNavbar == 2) {
        that.setData({ endpage: 1 })
        that.getEndWithdrawList()
      }
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    var tabtype = 0;

    if (that.data.currentNavbar == 0) {
      tabtype = that.data.allWithdrawResultDataStatus
    } else if (that.data.currentNavbar == 1) {
      tabtype = that.data.WithdrawResultingDataStatus
    } else if (that.data.currentNavbar == 2) {
      tabtype = that.data.WithdrawResultedDataStatus
    }

    if (that.data.upslideNoData && tabtype == false) {
      if (that.data.currentNavbar == 0) {
        that.setData({
          allpage: that.data.allpage + 1,//增加分页数
        })
        that.getAllWithdrawList()
      } else if (that.data.currentNavbar == 1) {
        that.setData({
          ingpage: that.data.ingpage + 1,//增加分页数
        })
        that.getIngWithdrawList()
      } else if (that.data.currentNavbar == 2) {
        that.setData({
          endpage: that.data.endpage + 1,//增加分页数
        })
        that.getEndWithdrawList()
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
	  return app.globalshare();
  },
  swichNav(e) {
    var that = this

    this.setData({
      currentNavbar: e.currentTarget.dataset.idx,
      //全部
      allWithdrawResultData: [],
      //提现中
      WithdrawResultingData: [],
      //已到账
      WithdrawResultedData: [],

      allWithdrawResultDataStatus: true,   //全部暂无数据， true为没有数据，false为有数据
      WithdrawResultingDataStatus: true, //提现中暂无数据，true为没有数据，false为有数据
      WithdrawResultedDataStatus: true,  //已到账暂无数据，true为没有数据，false为有数据

      allpage: 1,   // 设置加载的第几次，默认是第一次
      ingpage: 1,   // 设置加载的第几次，默认是第一次
      endpage: 1,   // 设置加载的第几次，默认是第一次

      upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
      upslideNoData: true,	//“没有数据”的变量，默认true，隐藏
    })

    //请求
    if (that.data.currentNavbar == 0) {
      that.getAllWithdrawList()
    } else if (that.data.currentNavbar == 1) {
      that.getIngWithdrawList()
    } else if (that.data.currentNavbar == 2) {
      that.getEndWithdrawList()
    }
  },
  //获取全部提现信息列表
  getAllWithdrawList:function(){
    var that = this
    var uid = app.globalData.userInfoId
    //开启加载
    if (that.data.ifRefresh == false) {
      that.setData({ upslideLoading: false })
    }
    //提交给服务端
    util.AJAX('Home/Troops/getAllWithdrawList', { uid: uid, page: that.data.allpage }, function (res) { //方法体
      //关闭加载
      if (that.data.ifRefresh == false) {
        that.setData({ upslideLoading: true })
      }

      if (res.data.StatusCode == 200) {  //成功
        var datalen = res.data.Info.length
        if (datalen) {
          if (that.data.allpage == 1) {
            that.setData({ allWithdrawResultData: res.data.Info, allWithdrawResultDataStatus:false })
          } else {
            var allWithdrawResultData = that.data.allWithdrawResultData
            var cs = allWithdrawResultData.concat(res.data.Info)
            that.setData({
              allWithdrawResultData: cs,
              allWithdrawResultDataStatus: false
            })
          }
        }else{
          //更新显示状态和减掉分页
          if (that.data.allpage > 1) {
            that.setData({
              allpage: that.data.allpage - 1,//减掉分页数
              upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
              upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
            })
          }
        }
      } else {	//失败
        //更新显示状态和减掉分页
        if (that.data.allpage > 1) {
          that.setData({
            allpage: that.data.allpage - 1,//减掉分页数
            upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
            upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
          })
        }
      }

      if (that.data.allWithdrawResultData.length == 0) {
        that.setData({ allWithdrawResultData: [], allWithdrawResultDataStatus: true })
      }

      //判断并关闭下拉刷新
      if (that.data.ifRefresh) {
        that.setData({ ifRefresh: false })
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }, 'POST')
  },
  //获取提现中提现信息列表
  getIngWithdrawList: function () {
    var that = this
    var uid = app.globalData.userInfoId
    //开启加载
    if (that.data.ifRefresh == false) {
      that.setData({ upslideLoading: false })
    }
    //提交给服务端
    util.AJAX('Home/Troops/getIngWithdrawList', { uid: uid, page: that.data.ingpage }, function (res) { //方法体
      //关闭加载
      if (that.data.ifRefresh == false) {
        that.setData({ upslideLoading: true })
      }

      if (res.data.StatusCode == 200) {  //成功
        var datalen = res.data.Info.length
        if (datalen) {
          if (that.data.ingpage == 1) {
            that.setData({ WithdrawResultingData: res.data.Info, WithdrawResultingDataStatus: false })
          } else {
            var WithdrawResultingData = that.data.WithdrawResultingData
            var cs = WithdrawResultingData.concat(res.data.Info)
            that.setData({
              WithdrawResultingData: cs,
              WithdrawResultingDataStatus: false
            })
          }
        }else{
          //更新显示状态和减掉分页
          if (that.data.ingpage > 1) {
            that.setData({
              ingpage: that.data.ingpage - 1,//减掉分页数
              upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
              upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
            })
          }
        }
      } else {	//失败
        //更新显示状态和减掉分页
        if (that.data.ingpage > 1) {
          that.setData({
            ingpage: that.data.ingpage - 1,//减掉分页数
            upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
            upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
          })
        }
      }

      if (that.data.WithdrawResultingData.length == 0) {
        that.setData({ WithdrawResultingData: [], WithdrawResultingDataStatus: true })
      }

      //判断并关闭下拉刷新
      if (that.data.ifRefresh) {
        that.setData({ ifRefresh: false })
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }, 'POST')
  },
  //获取已到账提现信息列表
  getEndWithdrawList: function () {
    var that = this
    var uid = app.globalData.userInfoId
    //var uid = 2
    //开启加载
    if (that.data.ifRefresh == false) {
      that.setData({ upslideLoading: false })
    }
    //提交给服务端
    util.AJAX('Home/Troops/getEndWithdrawList', { uid: uid, page: that.data.endpage }, function (res) { //方法体
      //关闭加载
      if (that.data.ifRefresh == false) {
        that.setData({ upslideLoading: true })
      }

      if (res.data.StatusCode == 200) {  //成功
        var datalen = res.data.Info.length
        if (datalen) {
          if (that.data.endpage == 1) {
            that.setData({ WithdrawResultedData: res.data.Info, WithdrawResultedDataStatus: true })
          } else {
            var WithdrawResultedData = that.data.WithdrawResultedData
            var cs = WithdrawResultedData.concat(res.data.Info)
            that.setData({
              WithdrawResultedData: cs,
              WithdrawResultedDataStatus: true
            })
          }
        }else{
          //更新显示状态和减掉分页
          if (that.data.endpage > 1) {
            that.setData({
              endpage: that.data.endpage - 1,//减掉分页数
              upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
              upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
            })
          }
        }
      } else {	//失败
        //更新显示状态和减掉分页
        if (that.data.endpage > 1) {
          that.setData({
            endpage: that.data.endpage - 1,//减掉分页数
            upslideLoading: true,  //"上拉加载"的变量，默认true，隐藏
            upslideNoData: false	//“没有数据”的变量，默认true，隐藏 
          })
        }
      }

      if (that.data.WithdrawResultedData.length == 0) {
        that.setData({ WithdrawResultedData: [], WithdrawResultedDataStatus: true })
      }

      //判断并关闭下拉刷新
      if (that.data.ifRefresh) {
        that.setData({ ifRefresh: false })
        // complete
        wx.hideNavigationBarLoading() //完成停止加载
        wx.stopPullDownRefresh() //停止下拉刷新
      }
    }, 'POST')
  }
})